package oop.db.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import oop.data.TestStructure;
import oop.db.Database;

public class TestStructureDAO {

	public static List<TestStructure> fetchById(long id) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT tstr_id, tstr_name, tstr_description, " +
					"tstr_author, tstr_topic, tstr_create_date " +
					"FROM tblTestStructure " +
					"WHERE tstr_id = ?";	
			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setLong(1, id);
			rs = stmt.executeQuery();
			return fromResultSet(rs);
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}
	
	public static List<TestStructure> fetch(int start, int length) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT tstr_id, tstr_name, tstr_description, " +
					"tstr_author, tstr_topic, tstr_create_date " +
					"FROM tblTestStructure " +
					"ORDER BY tstr_name ASC " +
					"LIMIT ?, ?";	
			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, start);
			stmt.setInt(2, length);
			rs = stmt.executeQuery();
			return fromResultSet(rs);
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}
	
	public static List<TestStructure> fetchCreateDateOrder(int start, int length) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT tstr_id, tstr_name, tstr_description, " +
					"tstr_author, tstr_topic, tstr_create_date " +
					"FROM tblTestStructure " +
					"ORDER BY tstr_create_date DESC " +
					"LIMIT ?, ?";	
			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, start);
			stmt.setInt(2, length);
			rs = stmt.executeQuery();
			return fromResultSet(rs);
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

	public static long count() throws SQLException {
		String sql = "SELECT COUNT(*) FROM tblTestStructure";
		return Database.get().count(sql);
	}
	
	public static List<TestStructure> fetchByTopic(long topicId, int start, int length) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT tstr_id, tstr_name, tstr_description, " +
					"tstr_author, tstr_topic, tstr_create_date " +
					"FROM tblTestStructure " +
					"WHERE tstr_topic = ? " +
					"ORDER BY tstr_name ASC " +
					"LIMIT ?, ?";
			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setLong(1, topicId);
			stmt.setInt(2, start);
			stmt.setInt(3, length);
			rs = stmt.executeQuery();
			return fromResultSet(rs);
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

	public static long countByTopic(long topicId) throws SQLException {
		String sql = "SELECT COUNT(*) FROM tblTestStructure WHERE tstr_topic = " + topicId;
		return Database.get().count(sql);
	}
	
	public static List<TestStructure> fetchByAuthor(long authorId, int start, int length) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT tstr_id, tstr_name, tstr_description, " +
					"tstr_author, tstr_topic, tstr_create_date " +
					"FROM tblTestStructure " +
					"WHERE tstr_author = ? " +
					"ORDER BY tstr_name ASC " +
					"LIMIT ?, ?";	
			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setLong(1, authorId);
			stmt.setInt(2, start);
			stmt.setInt(3, length);
			rs = stmt.executeQuery();
			return fromResultSet(rs);
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

	public static long countByAuthor(long authorId) throws SQLException {
		String sql = "SELECT COUNT(*) FROM tblTestStructure " +
				"WHERE tstr_author = " + authorId;
		return Database.get().count(sql);
	}

	private static List<TestStructure> fromResultSet(ResultSet rs)
			throws SQLException {
		List<TestStructure> structures = new ArrayList<TestStructure>();
		while (rs.next()) {
			structures.add(new TestStructure(rs.getLong(1), rs.getString(2), rs
					.getString(3), rs.getLong(4), rs.getLong(5), rs
					.getTimestamp(6)));
		}
		return structures;
	}
	
	public static void persist(TestStructure structure) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
				String sql = "UPDATE tblTestStructure SET " +
						"tstr_name = ?, tstr_description = ?, tstr_topic = ? " +
						"WHERE tstr_id = ?";

				conn = Database.get().getConnection();
				stmt = conn.prepareStatement(sql);
				stmt.setString(1, structure.getName());
				stmt.setString(2, structure.getDescription());
				stmt.setLong(3, structure.getTopic().getId());
				stmt.setLong(4, structure.getId());
				stmt.executeUpdate();
		} finally {
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}
	
	public static int drop(long id) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			String sql = "DELETE FROM tblTestStructure WHERE tstr_id = ?";
			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setLong(1, id);
			return stmt.executeUpdate();
		} finally {
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

	public static TestStructure create(String name, String description,
			long authorId, long topicId) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			String sql = "INSERT INTO tblTestStructure (tstr_name, " +
					"tstr_description, tstr_author, tstr_topic, " +
					"tstr_create_date) " +
					"VALUES (?, ?, ?, ?, ?)";
			
			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, name);
			stmt.setString(2, description);
			stmt.setLong(3, authorId);
			stmt.setLong(4, topicId);
			Date now = new Date();
			stmt.setTimestamp(5, new Timestamp(now.getTime()));
			stmt.executeUpdate();
			
			ResultSet rs = stmt.getGeneratedKeys();
			rs.next();
			long id = rs.getLong(1);
			return new TestStructure(id, name, description, authorId, topicId,
					now);
		} finally {
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

	public static List<TestStructure> fetchByName(String name, int limit) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT tstr_id, tstr_name, tstr_description, " +
					"tstr_author, tstr_topic, tstr_create_date " +
					"FROM tblTestStructure " + 
					"WHERE tstr_name LIKE ? " + 
					"ORDER BY tstr_name ASC " + 
					"LIMIT ?";

			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, name);
			stmt.setLong(2, limit);
			rs = stmt.executeQuery();

			return fromResultSet(rs);
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

}
